import { useLoginStore } from './login'

export const useUserStore = defineStore('user', () => {
  const login = useLoginStore()
  // 用户信息
  let user = $ref<any>({})
  let menuList = $ref([]) // 角色菜单列表
  let apiList = $ref([]) // 角色接口/模块列表
  let fieldList = $ref([]) // 角色字段列表
  let status = $ref<any>({
    portal: '',
    companyId: '',
    roleId: '',
    platform: null,
  })
  let portalList = $ref<any>([])
  let companys = $ref<any>([])
  let roles = $ref<any>([])
  let roleStamp = $ref(0)

  const clearUser = () => {
    menuList = []
    user = {}
    companys = []
    roles = []
  }

  const updateUser = (payload) => {
    user = payload
  }

  const updateUserDatas = (payload) => {
    user = payload.user
    companys = payload.companys
    roles = payload.roles
    portalList = payload.portalList
    status = payload.status
    menuList = payload.menuList
    roleStamp = payload.roleStamp
  }

  const updatePortalList = (payload) => {
    if (isNilOrEmpty(payload)) { return }
    portalList = payload.sort((a: any, b: any) => {
      return a.sortno < b.sortno ? 1 : -1
    })
  }

  const portalListShow = computed<any>(() => {
    if (isNilOrEmpty(portalList)) return []
    return portalList.filter((e: any) => e.sortno > -1)
  })

  const currPortal = computed<any>(() => {
    const portal = portalList.find((e: any) => e.platformCode === status.portal)
    if (isNilOrEmpty(portal)) {
      return {
        id: '',
        platformCode: '',
        platformName: '',
      }
    }
    return portal
  })
  const currCompany = computed<any>(() => {
    const company = companys.find((e: any) => e.id === status.companyId)
    if (isNilOrEmpty(company)) {
      return {
        id: '',
        companyCode: '',
        companyName: '',
        companyTitle: '',
      }
    }
    return company
  })
  const currRole = computed<any>(() => {
    const role = roles.find((e: any) => e.id === status.roleId)
    if (isNilOrEmpty(role)) {
      return {
        id: '',
        roleCode: '',
        roleName: '',
      }
    }
    return role
  })

  const isAdmin = computed<boolean>(() => {
    const role: any = roles.find((e: any) => e.id === status.roleId)
    return role && role.roleCode && role.roleCode === 'administrator'
  })
  //请求个人信息数据
  const useGetInfoData = async () => {
    const { data, status, error } = await Fetch('/api/s/t/user', {
      method: 'post',
      headers: { timestamp: new Date().getTime() },
    })
    const userConut: any = await Fetch(`/qqyapi/auth/user/userHomePage/${data.result?.dataList[0]?.id}`)
    if (status !== 200 || data.code !== 0) {
      return
    }
    updateUser({ ...data.result?.dataList[0], ...userConut.data?.result })
  }

  let infoData: any = $ref({})
  let conuts: any = $ref([])
  //请求单个个人主页
  const getInfo = async (userId) => {
    const userInfo = Fetch('/api/s/user/userinfo', {
      method: 'post',
      data: { id: userId },
    })
    const userConut = Fetch(`/qqyapi/auth/user/userHomePage/${userId}`)
    const [info, Conut] = await Promise.all([userInfo, userConut])
    infoData = info.data.result.dataList[0]
    conuts = Conut.data?.result ?? []
  }
  return {
    user: $$(user),
    infoData: $$(infoData),
    conuts: $$(conuts),
    menuList: $$(menuList),
    apiList: $$(apiList),
    fieldList: $$(fieldList),
    status: $$(status),
    portalList: $$(portalList),
    companys: $$(companys),
    roles: $$(roles),
    roleStamp: $$(roleStamp),
    portalListShow,
    currPortal,
    currCompany,
    currRole,
    isAdmin,
    updatePortalList,
    updateUser,
    updateUserDatas,
    clearUser,
    useGetInfoData,
    getInfo,
  }
}, { unistorage: { key: 'config-user' } })

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot))
